package com.qc31.baselib.custom

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.qc31.baselib.R
import com.qc31.baselib.databinding.LayouToolbarRadioBinding

class MRadioToolbar : LinearLayout {
    constructor(context: Context)
            : super(context)

    constructor(context: Context, attrs: AttributeSet?)
            : super(context, attrs) {
        init(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyle: Int)
            : super(context, attrs, defStyle) {
        init(context, attrs)
    }

    private lateinit var binding: LayouToolbarRadioBinding
    fun init(context: Context, attrs: AttributeSet?) {
        binding = LayouToolbarRadioBinding.inflate(LayoutInflater.from(context), this, true)

        val typedArray: TypedArray =
            context.obtainStyledAttributes(attrs, R.styleable.MToolbarRadio)
        val startTitle = typedArray.getString(R.styleable.MToolbarRadio_RStartTitle)
        val centerTitle = typedArray.getString(R.styleable.MToolbarRadio_RCenterTitle)
        val endTitle = typedArray.getString(R.styleable.MToolbarRadio_REndTitle)
        val isVisibility = typedArray.getBoolean(R.styleable.MToolbarRadio_RCenterGone, false)

        binding.rdbToolbarStart.text = startTitle
        binding.rdbToolbarCenter.text = centerTitle
        binding.rdbToolbarEnd.text = endTitle
        binding.rdbToolbarCenter.visibility = if (isVisibility) View.GONE else View.VISIBLE

        binding.ivToolbarStart.setOnClickListener {
            if (context is Activity) context.finish()
        }
        binding.rdbToolbarStart.setOnClickListener {
            callback?.startCallback()
        }
        binding.rdbToolbarCenter.setOnClickListener {
            callback?.centerCallback()
        }
        binding.rdbToolbarEnd.setOnClickListener {
            callback?.endCallback()
        }

        setBackgroundResource(R.color.color_379)
        typedArray.recycle()
    }

    fun setCheck(position: Int) {
        when (position) {
            0 -> binding.rdbToolbarStart.isChecked = true
            1 -> binding.rdbToolbarCenter.isChecked = true
            2 -> binding.rdbToolbarEnd.isChecked = true
        }
    }

    fun setCheckTwo(position: Int) {
        when (position) {
            0 -> binding.rdbToolbarStart.isChecked = true
            1 -> binding.rdbToolbarEnd.isChecked = true
        }
    }

    fun setStartTitle(titleId: Int) {
        binding.rdbToolbarStart.setText(titleId)
    }

    fun setCenterTitle(titleId: Int) {
        binding.rdbToolbarCenter.setText(titleId)
    }

    fun setEndTitle(titleId: Int) {
        binding.rdbToolbarEnd.setText(titleId)
    }

    fun setTitle(startId: Int, centerId: Int, endId: Int) {
        binding.rdbToolbarStart.setText(startId)
        binding.rdbToolbarCenter.setText(centerId)
        binding.rdbToolbarEnd.setText(endId)
    }

    fun setTitle(startId: Int, endId: Int) {
        binding.rdbToolbarStart.setText(startId)
        binding.rdbToolbarCenter.visibility = View.GONE
        binding.rdbToolbarEnd.setText(endId)
    }

    private var callback: IToolbarCallback? = null
    fun setCallBack(callback: IToolbarCallback) {
        this.callback = callback
    }

    interface IToolbarCallback {
        fun startCallback()
        fun centerCallback() {}
        fun endCallback()
    }

}